package com.liu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liu.entity.Medicine;
import com.liu.service.MedicineService;
import com.liu.vo.DataView;
import com.liu.vo.MedicineVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class MedicineController {

    @Autowired
    private MedicineService medicineService;

    @RequestMapping("/toMedicine")
    public String toMedicine(){
        return "admin/medicine";
    }

    /**
     *模糊查询药品
     * @param medicineVo
     * @return
     */
    @RequestMapping("/listMedicine")
    @ResponseBody
    public DataView listMedicine(MedicineVo medicineVo){
        IPage<Medicine> page = new Page<>(medicineVo.getPage(),medicineVo.getLimit());
        QueryWrapper<Medicine> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(medicineVo.getName() != null, "name",medicineVo.getName());
        queryWrapper.like(medicineVo.getType() != null, "type",medicineVo.getType());
        medicineService.page(page,queryWrapper);

        DataView dataView = new DataView(page.getTotal(),page.getRecords());
        return dataView;
    }

    @RequestMapping("/addMedicine")
    @ResponseBody
    public DataView addMedicine(Medicine medicine){
        DataView dataView = new DataView();
        boolean b = medicineService.saveOrUpdate(medicine);
        if(b) {
            dataView.setCode(200);
            dataView.setMsg("新增或者更改药品成功");
            return dataView;
        }
        dataView.setCode(100);
        dataView.setMsg("新增或者更改药品失败");
        return dataView;
    }

    @RequestMapping("/deleteMedicine")
    @ResponseBody
    public DataView deleteMedicine(Integer mid){
        DataView dataView = new DataView();
        boolean b = medicineService.removeById(mid);
        if(b){
            dataView.setCode(200);
            dataView.setMsg("药品删除成功");
            return dataView;
        }
        dataView.setCode(100);
        dataView.setMsg("药品删除失败");

        return  dataView;
    }
}
